home *** CD-ROM | disk | FTP | other *** search
- Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
- From: grantp@usa.pipeline.com(Pete Grant)
- Newsgroups: comp.lang.c++
- Subject: Re: Inheritance problem...and other stuff
- Date: 23 Mar 1996 14:49:22 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4j131i$qnu@news1.h1.usa.pipeline.com>
- References: <4j0ekj$9f6@cloner3.netcom.com>
- NNTP-Posting-Host: 38.8.60.7
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete Grant)
- X-Newsreader: Pipeline v3.5.0
-
- On Mar 23, 1996 09:01:07 in article <Inheritance problem...and other
- stuff>, 'pcgpe@ix.netcom.com(Mark Feldman)' wrote:
-
-
- >
- >Heya,
- >
- >I remember seeing a C++ FAQ once but I haven't had any luck finding it
- >again. If someone could point me to it I'd be grateful.
- >
- ftp from rtfm.mit.edu/pub/usenet-by-group/comp.lang.c++/*
- or you can search for old articles here as Marshall Cline
- posts the FAQ here once a month.
-
- >In the mean time I'll go ahead and ask my questions...at the risk of
- >asking something in the FAQ and getting flamed for it :)
- >
- >Here's a generic form of my problem. (BTW I use BC 3.1 and MSVC 4.0 and
- >solutions for either compiler would be fine.) Let's say I have a tree
- >of objects like this:
- [.. code deleted ...]
- >What I want to do however is determine info about the object which is
- >inherited from it's parents. Thus if my object is of type apple then
- >calling apple->isA("apple") should return true. apple->isA("fruit")
- >should also return true, but apple->isA("vegetable") should return
- >false. [.. more deleted ..]
-
- I believe you can come up with a workable solution by utilizing
- RTTI. AFAI remember, BC3.1 did not have RTTI implemented, but MSVC++4.0
- definitely does. Read up on typeid and related operators under the
- general topic of RTTI in your on-line documentation.
-
- >2nd Question:
- >
- >What's the standard way of storing such an array to a file and
- >retrieving it? I could always assign an ID number to each class type
- >and load them using case statements but this seems messy. I notice that
- >MFC and OWL seem to use tables to handle the dispatching of windows
- >messages. Would it be feasable to use this for saving the array info to
- >a file? Idealy I would like each function to inherit a serialization
- >function which they can overload to save and restore their data, but
- >how can I make the loading routine know what type of class to create in
- >the first place while still keeping the code tight and clean?
- >
- There's only one way: store the type information in the file. An
- enum is probably best; alternately, a name string would work also.
- When you read the data back in, you must check the type info and
- create an object of appropriate type.
-
- There are standard idioms (Coplien's book, for example) for simulating
- virtual constructors -- I have mixed feelings of those since they
- are just a thin veil for hiding what you are really doing. There's
- really no such thing as a virtual constructor in C++ and I feel a bit
- uneasy with idioms that try to make it look like they exist. But
- at the same time, I can see the benefits also.
-
- >Am I going about all this the wrong way? For some time now I've
- >considered myself a "good" C++ programmer. When I hit problems like
- >this I start to wonder if I'm really nothing more than a C programmer
- >with a C++ compiler. Feels like playing Zork on a Pentium.
- >
- >All help/suggestions/flames etc appreciated.
- >
- Well, suggestions mostly:
- Read, read, and when you finish, read some more.
- Here's excellent texts:
-
- Advanced C++ (I forget the exact title) by James Coplien
- Effective C++ by Scott Myers
- More Effective C++ by Scott Myers
-
- --
- Pete Grant
- Kalevi, Inc.
- Software Engineering & development
-